﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.PhieuChi
{
    public partial class frmXuLy : Form
    {
        public frmXuLy()
        {
            InitializeComponent();
        }
        
        #region Contructors
        string makho = "";
        string tenkho = "";
        public bool kiemtra1 = false;
        string hanhdong;
        bool checkClose = false;
        Entities.PhieuChi phieuchi;
        Entities.PhieuChi temp;
        Entities.PhieuChi pcin;
        DateTime date;
        private Entities.GetCombobox[] NoTaiKhoan;
        private Entities.GetCombobox[] CoTaiKhoan;


        #endregion

        #region check permission
        public bool CheckPer()
        {
            return true;
        }
        #endregion

        #region Check conflicts

        string maphieuchi = "";
        // Check Conflic Insert
        public bool CheckConflicInsert()
        {
            try
            {
                maphieuchi = getID();
                //if (SbiMaHoaDon.Text.ToString() == maphieuchi)
                if (phieuchi.MaPhieuChi == maphieuchi)
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return true;

            }

        }

        //Check Conflic Update
        public bool CheckConflicUpdate()
        {
            try
            {
                Entities.PhieuChi[] mangchitiet = Common.StaticVariable.InterfaceObject.SelectPhieuChi();

                for (int i = 0; i < mangchitiet.Length; i++)
                {
                    if (mangchitiet[i].MaPhieuChi == SbiMaHoaDon.Text)
                    {
                        return kiemtra(mangchitiet[i]);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        // kiểm tra
        public bool kiemtra(Entities.PhieuChi pc)
        {
            try
            {
                bool kt = true;
                if (temp.DoiTuong != pc.DoiTuong)
                {
                    cbbDoiTuong.Text = pc.DoiTuong;
                    kt = false;
                }
                if (temp.TenDoiTuong != pc.TenDoiTuong)
                {
                    lblTenDoiTuong.Text = pc.TenDoiTuong;
                    kt = false;
                }
                if (temp.DiaChi != pc.DiaChi)
                {
                    lblDiaChiDoiTuong.Text = pc.DiaChi;
                    kt = false;
                }
                if (temp.NguoiNhanTien != pc.NguoiNhanTien)
                {
                    txtNguoiNhanTien.Text = pc.NguoiNhanTien;
                    kt = false;
                }
                if (temp.MaDoiTuong != pc.MaDoiTuong)
                {
                    txtMaDoiTuong.Text = pc.MaDoiTuong;
                    kt = false;
                }
                if (temp.CoTaiKhoan != pc.CoTaiKhoan)
                {
                    cbbCoTaiKhoan.Text = pc.CoTaiKhoan;
                    kt = false;
                }
                if (temp.NoTaiKhoan != pc.NoTaiKhoan)
                {
                    cbbNoTaiKhoan.Text = pc.NoTaiKhoan;
                    kt = false;
                }
                if (temp.TongTienThanhToan != pc.TongTienThanhToan)
                {
                    txtSoTienThanhToan.Text = pc.TongTienThanhToan;
                    kt = false;
                }
                if (temp.GhiChu != pc.GhiChu)
                {
                    txtGhiChu.Text = pc.GhiChu.ToString();
                    kt = false;
                }
                return kt;
            }
            catch
            {
                return true;

            }

        }

        #endregion

        #region Nghiệp vụ

        // thoát
        public void Thoat()
        {
            try
            {
                checkClose = false;
                this.Close();
            }
            catch
            {
            }

        }

        // thêm
        public void Them()
        {
            try
            {
                if (Entitystatic.ViewFile().MaKho == "NULL" || Entitystatic.ViewFile().MaKho == string.Empty)
                {
                    MessageBox.Show("Bạn hãy cài địa điểm trước khi thêm mới");
                    return;
                }

                phieuchi = XuLyObject(Common.Constants.Insert);

                if (CheckConflicInsert())
                {
                    if (Common.StaticVariable.InterfaceObject.InsertUpdatePhieuChi(phieuchi))
                    {
                        checkClose = true;
                        UpdateDuNo();
                        this.Close();
                    }
                    else
                    {
                    }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG5(phieuchi.MaPhieuChi), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    lbMaPhieuChi.Text = maphieuchi;

                }

            }
            catch
            {
            }
        }

        // phai update du no vao bang chi tiet cong no KH or NCC
        public void UpdateDuNo()
        {
            try
            {
                if (cbbDoiTuong.Text == "Khách Hàng")
                {
                    Entities.ChiTietCongNoKH temp = new Entities.ChiTietCongNoKH("1",
                        txtMaDoiTuong.Text
                        , makho, txtSoTienThanhToan.Text, false);
                    Entities.ChiTietCongNoKH[] chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietCongNoKH(temp);

                    if (chitiet != null && chitiet.Length != 0)
                        temp.HanhDong = "Update"; // Cộng DuNo=DuNo + @DuN
                    else
                        temp.HanhDong = "Insert";
                    Common.StaticVariable.InterfaceObject.InsertUpdateChiTietCongNoKH(temp);

                }


                if (cbbDoiTuong.Text == "Nhà Cung Cấp")
                {
                    Entities.ChiTietCongNoNCC temp = new Entities.ChiTietCongNoNCC("1",
                        txtMaDoiTuong.Text
                        , makho, txtSoTienThanhToan.Text, false);
                    Entities.ChiTietCongNoNCC[] chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietCongNoNCC(temp);

                    if (chitiet != null && chitiet.Length != 0)
                        temp.HanhDong = "ThanhToan"; // // Trừ DuNo=DuNo - @DuNo
                    else
                        temp.HanhDong = "Insert";
                    Common.StaticVariable.InterfaceObject.InsertUpdateChiTietCongNoNCC(temp);
                }
            }
            catch { }
        }


        //in
        public void In()
        {
            try
            {
                if (pcin == null)
                {
                    MessageBox.Show("Bạn phải Ghi lại trước khi In", Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                this.Enabled = false;
                BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(pcin);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();
                    frm.kiemtra = false;
                }
                this.Enabled = true;
            }
            catch
            {
            }
        }

        #endregion

        #region Sự kiện windown

        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                makho = Entitystatic.ViewFile().MaKho;
                tenkho = Entitystatic.ViewFile().TenKho;

                btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnIn.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnGhiLai.MouseDown += new MouseEventHandler(MouseDown_Chung);

                txtSoTienThanhToan.MouseDown += new MouseEventHandler(txtsotienthanhtoan_MouseDown);
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            }
            catch
            { }
        }

        // Key down event
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.F2:
                        {
                            switch (hanhdong)
                            {
                                case Common.Constants.Insert:
                                    {
                                        Them();
                                        break;
                                    }
                            }
                            break;
                        }
                    case Keys.F3:
                        {
                           // In();
                            break;
                        }
                    case Keys.Escape:
                        {
                            Thoat();
                            break;
                        }

                }
            }
            catch
            {


            }

        }

        void Window_Closing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
                else
                {
                    
                   // if (!kiemtra1)
                    Select(hanhdong);
                }

            }
            catch
            {
            }
        }

        public delegate void SelectData(string hanhdong);
        public SelectData Select;

        // Select Chage cbbKhoanMuc
        Entities.KMThuChi[] kmtc;
        public void SelectChange_cbbKhoanMuc(object sender, EventArgs e)
        {
            try
            {
                int i = cbbKhoanMuc.SelectedIndex;
                if (i >= 0)
                {
                    for (int j = 0; j < kmtc.Length; j++)
                    {
                        if (cbbKhoanMuc.SelectedValue.ToString() == kmtc[j].MaKhoanMuc)
                        {
                            cbbCoTaiKhoan.Text = kmtc[j].CoTK.ToString();
                            cbbNoTaiKhoan.Text = kmtc[j].NoTK.ToString();
                            cbbDoiTuong.Text = kmtc[j].DoiTuong.ToString();
                            txtMaDoiTuong.ReadOnly = false;
                            txtMaDoiTuong.Text = "";
                            lblDiaChiDoiTuong.Text = "";
                            lblMaDoiTuong.Text = "";
                            lblTenDoiTuong.Text = "";
                            txtMaDoiTuong.Text = "<F4 - Tra cứu>";
                            return;
                        }

                    }
                }
            }
            catch
            {

            }
        }

        private void txtsotienthanhtoan_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                txtSoTienThanhToan.Text = String.Format("{0:0}", txtSoTienThanhToan.Text);
            }
            catch
            {

            }
        }

        //
        public void LoadInitial()
        {
            try
            {
                int i = cbbKhoanMuc.SelectedIndex;
                if (i >= 0)

                    kmtc = Common.StaticVariable.InterfaceObject.SelectKMThuChiString("True");
                
                    for (int j = 0; j < kmtc.Length; j++)
                    {
                        if (cbbKhoanMuc.SelectedValue.ToString() == kmtc[j].MaKhoanMuc)
                        {
                            cbbCoTaiKhoan.Text = kmtc[j].CoTK.ToString();
                            cbbNoTaiKhoan.Text = kmtc[j].NoTK.ToString();
                            cbbDoiTuong.Text = kmtc[j].DoiTuong.ToString();
                            txtMaDoiTuong.ReadOnly = false;
                            txtMaDoiTuong.Text = "";
                            lblDiaChiDoiTuong.Text = "";
                            lblMaDoiTuong.Text = "";
                            lblTenDoiTuong.Text = "";
                            txtMaDoiTuong.Text = "<F4 - Tra cứu>";
                            return;
                        }

                    }
                
            }
            catch
            {

            }
        }

        #endregion

        #region Form Xử lý Phiếu Chi
        // xử lý khi là xem lại
        public frmXuLy(Entities.PhieuChi phieuchi, string hanhdong)
        {
            try
            {
                InitializeComponent();
                this.hanhdong = hanhdong;
                temp = phieuchi;
                pcin = phieuchi;
                panel_HienThiLeft.Enabled = false;
                btnGhiLai.Enabled = false;
                // get data into combobox
                ComBoBox_Chung();

                // 
                XuLyDuLieu(phieuchi);
                HienThiComBoBoxKhoanMuc(phieuchi.KhoanMuc);
                HienThiComBoBoxCoTaiKhoan(phieuchi.CoTaiKhoan);
                HienThiComBoBoxDoiTuong(phieuchi.DoiTuong);
                HienThiComBoBoxNoTaiKhoan(phieuchi.NoTaiKhoan);
                //
            }
            catch
            {  }
        }

        // xử lý khi là thêm mới
        public frmXuLy(string hanhdong)
        {
            try
            {
                InitializeComponent();
                this.hanhdong = hanhdong;
                btnIn.Enabled = false;
                lbMaPhieuChi.Text = getID();
                ComBoBox_Chung();
                LoadInitial();
            }
            catch
            {
            }
        }

        #endregion

        #region Combobox

        public void ComBoBox_Chung()
        {
            try
            {
                GetDataComboboxKhoanMuc();
                GetDataComboboxDoiTuong();
                GetDataComboboxCoTaiKhoan();
                GetDataComboboxNoHienThoi();
                

                kmtc = Common.StaticVariable.InterfaceObject.SelectKMThuChiString("True");

                cbbKhoanMuc.SelectedValueChanged += new EventHandler(SelectChange_cbbKhoanMuc);
            }
            catch
            {


            }
        }



        // display on combobox with condition khoan mục
        public void HienThiComBoBoxKhoanMuc(string khoanmuc)
        {
            try
            {
                for (int i = 0; i < cbbKhoanMuc.Items.Count; i++)
                {
                    Entities.KMThuChi id = (Entities.KMThuChi)cbbKhoanMuc.Items[i];
                    if (id.TenKhoanMuc == khoanmuc)
                    {
                        cbbKhoanMuc.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch
            { }

        }

        // display on combobox with condition NoTaiKhoan
        public void HienThiComBoBoxNoTaiKhoan(string notaikhoan)
        {
            try
            {
                for (int i = 0; i < cbbNoTaiKhoan.Items.Count; i++)
                {
                    Entities.TKKeToan id = (Entities.TKKeToan)cbbNoTaiKhoan.Items[i];
                    if (id.MaTKKeToan == notaikhoan)
                    {
                        cbbNoTaiKhoan.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch
            {


            }

        }

        // display on combobox with condition Có Taì Khoan
        public void HienThiComBoBoxCoTaiKhoan(string cotaikhoan)
        {
            try
            {
                for (int i = 0; i < cbbCoTaiKhoan.Items.Count; i++)
                {
                    Entities.TKKeToan id = (Entities.TKKeToan)cbbCoTaiKhoan.Items[i];
                    if (id.MaTKKeToan == cotaikhoan)
                    {
                        cbbCoTaiKhoan.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch
            {


            }

        }

        // display on combobox with condition Đối tượng
        public void HienThiComBoBoxDoiTuong(string doituong)
        {
            try
            {
                for (int i = 0; i < cbbDoiTuong.Items.Count; i++)
                {
                    Entities.GetCombobox id = (Entities.GetCombobox)cbbDoiTuong.Items[i];
                    if (id.TenCotTenHienThi == doituong)
                    {
                        cbbDoiTuong.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch
            {


            }

        }


        // load Khoan muc in table khoan muc thu chi , display by combobox
        private void GetDataComboboxKhoanMuc()
        {
            try
            {
                Entities.KMThuChi[] kmtc = Common.StaticVariable.InterfaceObject.SelectKMThuChiString("True");

                Common.Utilities.XuLyComboBox(cbbKhoanMuc, kmtc, "TenKhoanMuc", "MaKhoanMuc");

            }
            catch { }
        }

        // load Nợ hiện thời in table khoan muc thu chi , display by combobox
        private void GetDataComboboxNoHienThoi()
        {
            try
            {
                Entities.TKKeToan[] tkkt =Common.StaticVariable.InterfaceObject.SelectCoNo("CO");
                Common.Utilities.XuLyComboBox(cbbNoTaiKhoan, tkkt, "MaTKKeToan", "MaTKKeToan");
            }
            catch { }
        }

        // load Có tài khoản  in table khoan muc thu chi , display by combobox
        private void GetDataComboboxCoTaiKhoan()
        {
            try
            {
                Entities.TKKeToan[] tkkt =Common.StaticVariable.InterfaceObject.SelectCoNo("NO");

                Common.Utilities.XuLyComboBox(cbbCoTaiKhoan, tkkt, "MaTKKeToan", "MaTKKeToan");

            }
            catch { }
        }

        // load ten doi tuong in table khoan muc thu chi , display by combobox
        private Entities.GetCombobox[] combobox_tendoituong;
        private void GetDataComboboxDoiTuong()
        {
            try
            {
                cbbDoiTuong.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "KMThuChi";
                pb.TenCotKhoaChinh = "TenKhoanMuc";
                pb.TenCotTenHienThi = "DoiTuong";
                combobox_tendoituong = Common.StaticVariable.InterfaceObject.SelectCombox(pb);

                Common.Utilities.XuLyComboBox(cbbDoiTuong, combobox_tendoituong, Common.Constants.TenCotTenHienThi, Common.Constants.TenCotKhoaChinh);
            }
            catch { }
        }

        #endregion

        #region  Mouse down event
        private void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button Img = (Button)sender;
                switch (Img.Name)
                {
                    case Common.Constants.btnThoat:
                        {
                            Thoat();
                            break;
                        }
                    case Common.Constants.btnGhiLai:
                        {
                            if (hanhdong == Common.Constants.Insert)
                            {
                                Them();
                            }
                            break;
                        }
                    case Common.Constants.btnIn:
                        {
                            In();
                            break;
                        }
                }
            }
            catch
            {
            }

        }
        #endregion

        #region Control Event

        #region KeyDown
        private void cbbKhoanMuc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtMaDoiTuong.Focus();
            }
        }
        private void txtDoiTuong2_KeyDown(object sender, KeyEventArgs e)
        {
            txtMaDoiTuong.Text = "";
            if (e.KeyCode == Keys.Enter)
            {
                txtMaDoiTuong.Text = "";
                txtNguoiNhanTien.Focus();
            }
            if (e.KeyCode == Keys.F4)
            {
                try
                {
                    if (cbbDoiTuong.Text == "Khách Hàng")
                    {
                        TimKiem.frmKhachHang a = new TimKiem.frmKhachHang("KH");
                        a.ShowDialog();
                        if (TimKiem.frmKhachHang.khachhang != null)
                        {
                            lblMaDoiTuong.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                            txtMaDoiTuong.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                            lblTenDoiTuong.Text = TimKiem.frmKhachHang.khachhang.Ten;
                            lblDiaChiDoiTuong.Text = TimKiem.frmKhachHang.khachhang.DiaChi;
                            txtMaDoiTuong.Text = TimKiem.frmKhachHang.khachhang.MaKhachHang;
                            TimKiem.frmKhachHang.khachhang = null;
                        }
                    }
                    if (cbbDoiTuong.Text == "Nhân Viên")
                    {
                        TimKiem.frmTimKiemNhanVien TimKiemKhachHang = new TimKiem.frmTimKiemNhanVien();
                        TimKiemKhachHang.ShowDialog();
                        if (TimKiem.frmTimKiemNhanVien.nhanvien != null)
                        {
                            lblMaDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.MaNhanVien;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.MaNhanVien;
                            lblTenDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.TenNhanVien;
                            lblDiaChiDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.DCThuongTru;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNhanVien.nhanvien.MaNhanVien;
                            TimKiem.frmTimKiemNhanVien.nhanvien = null;
                        }
                    }

                    if (cbbDoiTuong.Text == "Nhà Cung Cấp")
                    {
                        TimKiem.frmTimKiemNCC TimKiemNhaCungCap = new TimKiem.frmTimKiemNCC();
                        TimKiemNhaCungCap.ShowDialog();
                        if (TimKiem.frmTimKiemNCC.NhaCungCap != null)
                        {
                            lblMaDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.MaNhaCungCap;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.MaNhaCungCap;
                            lblTenDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.TenNhaCungCap;
                            lblDiaChiDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.DiaChi;
                            txtMaDoiTuong.Text = TimKiem.frmTimKiemNCC.NhaCungCap.MaNhaCungCap;
                            TimKiem.frmTimKiemNCC.NhaCungCap = null;
                        }
                    }
                }
                catch 
                {
                }
  
            }
        }
        private void txtNguoiNop_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cbbNoTaiKhoan.Focus();
            }
        }
        private void cbbNoTaiKhoan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cbbCoTaiKhoan.Focus();
            }
        }
        private void cbbCoTaiKhoan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtTKNganHang.Focus();
            }
        }
  
        private void txtTKNganHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSoTienThanhToan.Focus();
            }
        }

        private void txtSoTienThanhToan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGhiChu.Focus();
            }
        }

        private void cbbTenTienTe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSoTienThanhToan.Focus();
            }
        }  
        private void txtGhiChu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cbbKhoanMuc.Focus();
            }
        }
        private void txtDoiTuong1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtNguoiNhanTien.Focus();
            }
        }

        #endregion

        #region xử lý ID
  
        #region GetID
        public  string getID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "PhieuChi";
                Entities.GetID get =Common.StaticVariable.InterfaceObject.SelectID(id);

                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "PC_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "PC_0001";
            }
            return code;
        }
        #endregion

        #endregion

        #region check validate
        public bool Validate()
        {
            try
            {
                if (txtNguoiNhanTien.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbNguoiNhanTien.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtNguoiNhanTien.Focus();
                    return false;
                }
                if (cbbKhoanMuc.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbKhoanMuc.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbKhoanMuc.Focus();
                    return false;
                }
                if (cbbDoiTuong.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbDoiTuong.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbDoiTuong.Focus();
                    return false;
                }
                if (cbbCoTaiKhoan.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbCoTaiKhoan.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbCoTaiKhoan.Focus();
                    return false;
                }
                if (cbbNoTaiKhoan.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbNoTaiKhoan.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbbNoTaiKhoan.Focus();
                    return false;
                }
              
                if (txtSoTienThanhToan.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbSoTienThanhToan.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtSoTienThanhToan.Focus();
                    return false;
                }
           
                return true;
            }
            catch 
            {
                return true;
                
            }
       
        }
        #endregion

        #region Xử lý dữ liệu
        public void XuLyDuLieu(Entities.PhieuChi phieuchi)
        {
            try
            {
                lbMaPhieuChi.Text = phieuchi.MaPhieuChi;
                txtMaDoiTuong.Text = phieuchi.MaDoiTuong;
                lblMaDoiTuong.Text = phieuchi.MaDoiTuong;
                cbbKhoanMuc.Text = phieuchi.KhoanMuc;
                cbbDoiTuong.Text = phieuchi.DoiTuong;
                lblTenDoiTuong.Text = phieuchi.TenDoiTuong;
                lblDiaChiDoiTuong.Text = phieuchi.DiaChi;
                phieuchi.MaSoThue = "";
                txtNguoiNhanTien.Text = phieuchi.NguoiNhanTien;
                cbbCoTaiKhoan.Text = phieuchi.CoTaiKhoan;
                cbbNoTaiKhoan.Text = phieuchi.NoTaiKhoan;
                txtSoTienThanhToan.Text = Common.Utilities.FormatMoney(phieuchi.TongTienThanhToan);
                txtGhiChu.Text = phieuchi.GhiChu;
            }
            catch 
            { }
        }
        #endregion

        #region Insert data of phiếu chi into database
        public Entities.PhieuChi XuLyObject(string hanhdong)
        {
            try
            {
                if (Validate())
                {
                    Entities.PhieuChi phieuchi = new Entities.PhieuChi();

                    phieuchi.HanhDong = hanhdong;
                    phieuchi.MaPhieuChi = lbMaPhieuChi.Text.ToString();
                    phieuchi.NgayLap = DateTime.Parse(Common.StaticVariable.InterfaceObject.SelectDate().ToString("MM/dd/yyyy"));
                    phieuchi.MaDoiTuong = lblMaDoiTuong.Text.ToString();
                    phieuchi.KhoanMuc = cbbKhoanMuc.Text.ToString();
                    phieuchi.DoiTuong = cbbDoiTuong.Text.ToString();
                    phieuchi.TenDoiTuong = lblTenDoiTuong.Text.ToString();
                    phieuchi.DiaChi = lblDiaChiDoiTuong.Text.ToString();
                    phieuchi.MaSoThue = "123";
                    phieuchi.NguoiNhanTien = txtNguoiNhanTien.Text.ToString();
                    phieuchi.MaTKNganHang = "123";
                    phieuchi.CoTaiKhoan = cbbCoTaiKhoan.Text.ToString();
                    phieuchi.NoTaiKhoan = cbbNoTaiKhoan.Text.ToString();
                    phieuchi.TongTienThanhToan = txtSoTienThanhToan.Text;
                    phieuchi.MaKho = Entitystatic.ViewFile().MaKho;
                    phieuchi.MaNhanVien = UtilitiesVariable.User.MaNhanVien;
                    phieuchi.GhiChu = txtGhiChu.Text.ToString();
                    phieuchi.Deleted = false;
                    phieuchi.TaiKhoan = UtilitiesVariable.User;

                    return phieuchi;
                }
                return null;
            }
            catch
            {
                return null;
            }

        }
        #endregion


        private void txtSoTienThanhToan_TextChanged(object sender, EventArgs e)
        {
                TextBox temp = (TextBox)sender;
                int vt = temp.SelectionStart;
                string str = new string(temp.Text.ToList<char>().Where(c => char.IsNumber(c)).ToArray<char>());

                temp.Text = str;
                temp.SelectionStart = vt;
        }

        private void txtMaDoiTuong_MouseLeave(object sender, EventArgs e)
        {
            if (txtMaDoiTuong.Text == "")
            {
                txtMaDoiTuong.Text = "<F4 - Tra cứu>";
            }
        }

        private void txtMaDoiTuong_MouseMove(object sender, MouseEventArgs e)
        {
            if (txtMaDoiTuong.Text == "<F4 - Tra cứu>")
            {
                txtMaDoiTuong.Text = "";
            }
        }

        private void txtNguoiNhanTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                Common.ValidateControl.CheckNumber(e);
            }
            catch
            {
            }
        }
        #endregion



    }
}
